//
//  EncyWriteComment6PlusViewController.m
//  安好
//
//  Created by lianchuang on 15/1/13.
//  Copyright (c) 2015年 lianchuangbrother. All rights reserved.
//

#import "EncyWriteComment6PlusViewController.h"

@interface EncyWriteComment6PlusViewController ()<UITextFieldDelegate,UITextViewDelegate>

@property(nonatomic,assign)NSInteger comEnviron;
@property(nonatomic,assign)NSInteger comEffect;
@property(nonatomic,assign)NSInteger comService;



@end

@implementation EncyWriteComment6PlusViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [ClassMethodsViewController colorWithHexString:BACK_BACKGROUNDCOLOR_16];
    [self makeUI];
    [self makeNavBarView];
    [self textxviewedit];
}

#pragma mark - 设置导航条
-(void)makeNavBarView
{
    self.navigationItem.title = @"点评";
    //贴图
    UIBarButtonItem *backBtnImg = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"公用_返回.png"] style:UIBarButtonItemStylePlain target:self action:@selector(backMoreVC)];
    backBtnImg.tintColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem = backBtnImg;
}
-(void)backMoreVC
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)textxviewedit
{
    
    self.comTextView.delegate = self;
    self.comTextView.userInteractionEnabled = YES;
}

-(void)makeUI
{
    
    for (int i = 0; i < 5; i++) {
        UIButton * starButton = [UIButton buttonWithType:UIButtonTypeCustom];
        starButton.frame = CGRectMake(i*(15+12), 0, 20, 20);
        starButton.tag = 11+i;
        [starButton setImage:[UIImage imageNamed:@"stargray"] forState:UIControlStateNormal];
        [starButton addTarget:self action:@selector(FirststarButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
        [self.comFirstStarView addSubview:starButton];
        UIView * view = [[UIView alloc] initWithFrame:starButton.bounds];
        [starButton addSubview:view];
        view.userInteractionEnabled = NO;
    }
    for (int i = 0; i < 5; i++) {
        UIButton * starButton = [UIButton buttonWithType:UIButtonTypeCustom];
        starButton.frame = CGRectMake(i*(15+12), 0, 15, 15);
        starButton.tag = 21+i;
        [starButton setImage:[UIImage imageNamed:@"stargray"] forState:UIControlStateNormal];
        [starButton addTarget:self action:@selector(SecondstarButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
        [self.comSecStarView addSubview:starButton];
        UIView * view = [[UIView alloc] initWithFrame:starButton.bounds];
        [starButton addSubview:view];
        view.userInteractionEnabled = NO;
    }
    for (int i = 0; i < 5; i++) {
        UIButton * starButton = [UIButton buttonWithType:UIButtonTypeCustom];
        starButton.frame = CGRectMake(i*(15+12), 0, 15, 15);
        starButton.tag = 31+i;
        [starButton setImage:[UIImage imageNamed:@"stargray"] forState:UIControlStateNormal];
        [starButton addTarget:self action:@selector(ThirdstarButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
        [self.comThirdStarView addSubview:starButton];
        UIView * view = [[UIView alloc] initWithFrame:starButton.bounds];
        [starButton addSubview:view];
        view.userInteractionEnabled = NO;
    }
    

    [self.comFinishButton addTarget:self action:@selector(comFinishButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    self.comFinishButton.backgroundColor = [UIColor colorWithRed:61/255.0 green:179/255.0 blue:230/255.0 alpha:1];
    
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapclicked:)];
    [self.comStarbackView addGestureRecognizer:tap];
    
    [self.view addGestureRecognizer:tap];
    
    
    
    
}
-(void)tapclicked:(UITapGestureRecognizer*)tap
{
    [self.comTextView resignFirstResponder];
    
}
-(void)comFinishButtonClicked:(UIButton*)btn
{
    
    NSString * envirScore = [NSString stringWithFormat:@"%ld",self.comEnviron];
    NSString * effectScore = [NSString stringWithFormat:@"%ld",self.comEffect];
    NSString * serciceScore =[NSString stringWithFormat:@"%ld",self.comService];
    
    
    if (self.comTextView.text.length <= 0) {
        [self showCustomHudSingleLine:@"请添加评论内容"];
        return;
    }
    if ([envirScore intValue]==0 ||[effectScore intValue]==0||[serciceScore intValue]==0) {
        [self showCustomHudSingleLine:@"分数评价不完整"];
        return;
    }
    
    NSString *postURL = [NSString stringWithFormat:@"%@/wiki/addHospitalComment/",kBaseURL];
    //post参数列表
    NSDictionary *parameters = @{@"platform": kPlatform,@"version":kVersion,self.commentKey:self.commentId,@"comment":self.comTextView.text,@"environment_score":envirScore,@"effect_score":effectScore,@"sercice_score":serciceScore,@"type":self.type};
    [self showActionView];
    //网络请求管理者
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager POST:postURL parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        MyNSLog("成功");
        [self hiddenActionView];
        NSNumber * statusNum = [responseObject objectForKey:@"status"];
        NSNumberFormatter* numberFormatter = [[NSNumberFormatter alloc] init];
        NSString * statusString = [numberFormatter stringFromNumber:statusNum];
        if ([statusString isEqualToString:@"0"]){
            MyNSLog("%@", responseObject[@"msg"]);
        }else
        {
            [self.delegate plusReloadCommentListView];
            [self.navigationController popViewControllerAnimated:YES];
            
            
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [self hiddenActionView];
    }];
    
}
-(void)FirststarButtonClicked:(UIButton*)button
{
    [self.comTextView resignFirstResponder];
    self.comEnviron = button.tag-10;
    for (NSInteger i = 0; i < 5; i++) {
        UIButton * btn = (UIButton*)[self.view viewWithTag:i+11];
        if (i<=button.tag-11) {
            [btn setImage:[UIImage imageNamed:@"staryellow"] forState:UIControlStateNormal];
        }else{
            [btn setImage:[UIImage imageNamed:@"stargray"] forState:UIControlStateNormal];
        }
    }
    
}
-(void)SecondstarButtonClicked:(UIButton*)button
{
    [self.comTextView resignFirstResponder];
    self.comEffect = button.tag-20;
    for (NSInteger i = 0; i < 5; i++) {
        UIButton * btn = (UIButton*)[self.view viewWithTag:i+21];
        if (i<=button.tag-21) {
            [btn setImage:[UIImage imageNamed:@"staryellow"] forState:UIControlStateNormal];
        }else{
            [btn setImage:[UIImage imageNamed:@"stargray"] forState:UIControlStateNormal];
        }
    }
    
    
}
-(void)ThirdstarButtonClicked:(UIButton*)button
{
    [self.comTextView resignFirstResponder];
    self.comService = button.tag-30;
    for (NSInteger i = 0; i < 5; i++) {
        UIButton * btn = (UIButton*)[self.view viewWithTag:i+31];
        if (i<=button.tag-31) {
            [btn setImage:[UIImage imageNamed:@"staryellow"] forState:UIControlStateNormal];
        }else{
            [btn setImage:[UIImage imageNamed:@"stargray"] forState:UIControlStateNormal];
        }
    }
    
}

-(void)textViewDidChange:(UITextView *)textView
{
    if (textView.text.length>0) {
        self.placeholderlable.hidden = YES;
    }else
    {
        self.placeholderlable.hidden = NO;
    }
    
}
-(void)textViewDidEndEditing:(UITextView *)textView
{
    [textView resignFirstResponder];
}
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        return NO;
    }else
    {
        return YES;
    }
}









- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
